//
//  HomePageCell.m
//  ZhiHuDaily
//
//  Created by 吴宜昊 on 2022/2/5.
//

#import "HomePageCell.h"

@interface HomePageCell ()
@property(nonatomic,strong) UILabel * labelTitle;
@property(nonatomic,strong) UILabel * labelReader;
@property(nonatomic,strong) UIImageView * ivIcon;
@end
@implementation HomePageCell

#pragma mark - override
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupUI];
    }
    return self;
}

-(void)updateConstraints {
    [self remakeMas];
    [super updateConstraints];
}

#pragma mark - remakeMas
-(void) remakeMas {
    WeakSelf(self);
    [self.ivIcon mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.width.height.equalTo(@70);
        make.right.equalTo(@-16);
        make.top.equalTo(@16);
        make.bottom.equalTo(@-16);
    }];
    
    [self.labelTitle mas_remakeConstraints:^(MASConstraintMaker *make) {
        StrongSelf(weakSelf);
        make.left.equalTo(@16);
        make.top.equalTo(@16);
        make.right.lessThanOrEqualTo(strongSelf.ivIcon.mas_left).offset(-6);
    }];
    
    [self.labelReader mas_remakeConstraints:^(MASConstraintMaker *make) {
        StrongSelf(weakSelf);
        make.left.equalTo(@16);
        make.top.equalTo(strongSelf.labelTitle.mas_bottom).offset(4);
    }];
}

#pragma mark - setupUI
-(void) setupUI {
    [self.contentView addSubview:self.labelTitle];
    [self.contentView addSubview:self.labelReader];
    [self.contentView addSubview:self.ivIcon];
    [self setNeedsUpdateConstraints];
    
    [self setSelectionStyle:UITableViewCellSelectionStyleNone];
}

#pragma mark - lazy
-(UILabel *) labelTitle {
    if (!_labelTitle) {
        _labelTitle = [UILabel new];
        _labelTitle.font = FontPingFangMedium(17);
        _labelTitle.textColor = UIColor.blackColor;
        _labelTitle.numberOfLines = 2;
        _labelTitle.textAlignment = NSTextAlignmentJustified;
        _labelTitle.text = @" ";
    }
    return _labelTitle;
}

-(UILabel *) labelReader {
    if (!_labelReader) {
        _labelReader = [UILabel new];
        _labelReader.font = FontPingFangRegular(13);
        _labelReader.textColor = HEXCOLOR(0xA8A8A8);
        _labelReader.text = @" ";
    }
    return _labelReader;
}

-(UIImageView *) ivIcon {
    if (!_ivIcon) {
        _ivIcon = [UIImageView new];
        _ivIcon.image = ImageDefault;
        _ivIcon.layer.cornerRadius = 8;
        _ivIcon.layer.masksToBounds = YES;
    }
    return _ivIcon;
}

-(void)setModel:(HomePageM *) model {
    _model = model;
    
    self.labelTitle.text = model.title;
    self.labelReader.text = model.hint;
    
    if (model.images && model.images.isNotEmpty) {
        NSURL * url = [NSURL URLWithString:model.images.firstObject];
        [self.ivIcon sd_setImageWithURL:url placeholderImage:ImageDefault];
    }
}
@end
